package com.example.farmeri.presenter.user;

import android.util.Log;

import com.example.farmeri.data.RetrofitManager;
import com.example.farmeri.http.API;
import com.example.farmeri.pojo.FarmDataT;
import com.example.farmeri.pojo.Role;
import com.example.farmeri.pojo.User;
import com.example.farmeri.presenter.base.BasePresenter;
import com.example.farmeri.presenter.strategy.IAddStrategyContract;
import com.example.farmeri.tools.ResponseUtils;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;
import okhttp3.FormBody;
import okhttp3.RequestBody;

public class ViewUserPresenter extends BasePresenter<IViewUserContract> {

    public ViewUserPresenter(IViewUserContract iViewUserContract) {
        super(iViewUserContract);
    }

    public void getUserInfo(String id){
        getAPI().viewUser(id)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ResponseUtils<User>>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull ResponseUtils<User> responseUtils) {
                        if (responseUtils.getCode() == ResponseUtils.CODE_SUCCESS){
                            getView().onUserInfoSuccess(responseUtils.getData());
                        }else {
                            getView().showMessage(responseUtils.getMessage());
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        showErrorMsg();
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public void submit(Map<String, String> map) {
        FormBody.Builder formBuilder = new FormBody.Builder();
        for (Map.Entry<String, String> entry : map.entrySet()) {
            formBuilder.add(entry.getKey(),entry.getValue());
        }
        getAPI().updateUserInfo(formBuilder.build())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ResponseUtils>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull ResponseUtils responseUtils) {
                        if (responseUtils.getCode() == ResponseUtils.CODE_SUCCESS){
                            getView().onUpdateUserInfoSuccess("");
                        }else {
                            getView().showMessage(responseUtils.getMessage());
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        showErrorMsg();
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
